(function() {
	'use strict';

	angular.module('cf-content')

	.directive('business', ['$timeout', "$window", function($timeout, $window,EVENT) {
		return {
			restrict: 'AE',
			templateUrl:'app/page/business/business.html',
			// replace:true,
			controller:business,
			controllerAs:'businessCtrl'
		}
	}])

	
	.controller('BusinessModalInstanceCtrl',BusinessModalInstanceCtrl)

	BusinessModalInstanceCtrl.$inject=['$scope', '$uibModalInstance','items'];

	function BusinessModalInstanceCtrl($scope, $uibModalInstance,items) {

		  $scope.item=items;

		  $scope.ok = function () {
		    $uibModalInstance.close(true);
		  };

		  $scope.cancel = function () {
		    $uibModalInstance.dismiss(false);
		  };
	}
	


	business.$inject=['$scope','$uibModal','$timeout','Api']

	function business($scope,$uibModal,$timeout,Api){
		var vm = this;
		initData();
		vm.fieldFirstList={
			tooltitle:'一级行业',
			insertdata:{
				name:''
			},
			selectedId:'',
			addible:false,
			wait:true,
			load:load,
			minus:minus,
			edit:edit,
			save:save,
			data:[]
		}
		vm.fieldSecondList={
			tooltitle:'二级行业',
			insertdata:{
				name:''
			},
			wait:true,
			selectedId:'',
			addible:false,
			minus:minus,
			load:childload,
			edit:edit,
			save:childsave,
			data:[]
		}
		vm.fieldThridList={
			tooltitle:'三级行业',
			insertdata:{
				name:''
			},
			wait:true,
			selectedId:'',
			addible:false,
			load:prechildload,
			minus:minus,
			edit:edit,
			save:thridchildsave,
			data:[]
		}
		vm.fieldFourList={
			tooltitle:'四级行业',
			insertdata:{
				name:''
			},
			wait:true,
			selectedId:'',
			addible:false,
			minus:minus,
			edit:edit,
			save:fourchildsave,
			data:[]
		}
		function minus(obj){
			var selectedData;
			console.log(obj)
			for(var i in obj.data){
				if(obj.data[i].id==obj.selectedId){
					selectedData=obj.data[i];
					break;
				}
			}
			vm.EnsureModel('sm',selectedData.name,function(data){
				if(data){
					Api.Field.Delete({field_id:selectedData.id}).$promise.then(function(data){
						vm.InfoModel('sm','删除成功','success')
						initData();
					})
				}
			})
		}

		function edit(obj){
			for(var i in obj.data){
				if(obj.data[i].id==obj.selectedId){
					obj.data[i].editable=true;
					break;
				}
			}
		}

		function save(obj){
			var selectedData;
			if(obj.addible){
				if(obj.insertdata.name==''){
					vm.InfoModel('sm','请填写字段名')
					return;
				}
				obj.addible=false;
				Api.Field.Put({name:obj.insertdata.name}).$promise.then(function(data){
						obj.insertdata.name='';
						initData();
				})
			}
			else{
				for(var i in obj.data){
					if(obj.data[i].id==obj.selectedId){
						selectedData=obj.data[i];
						obj.data[i].editable=false;
						break;
					}
				}
				console.log(selectedData);
				Api.Field.Update({field_id:selectedData.id,name:selectedData.name}).$promise.then(function(data){
						if(data.code==0)
	                     	vm.InfoModel('sm','操作成功','success')
	                  	else
	                    	vm.InfoModel('sm','操作失败','error')
						initData();
				})
			}
		}

		function childsave(obj){
			var selectedData;
			if(obj.addible){
				if(obj.insertdata.name==''){
					vm.InfoModel('sm','请填写字段名')
					return;
				}
				obj.addible=false;
				Api.Field.Put({parent_field_id:vm.fieldFirstList.selectedId,name:obj.insertdata.name}).$promise.then(function(data){
						if(data.code==0)
	                     	vm.InfoModel('sm','操作成功','success')
	                  	else
	                    	vm.InfoModel('sm','操作失败','error')
						obj.insertdata.name=''
						initSecondData(vm.fieldFirstList.selectedId)
				})
			}
			else{
				for(var i in obj.data){
					if(obj.data[i].id==obj.selectedId){
						selectedData=obj.data[i];
						obj.data[i].editable=false;
						break;
					}
				}
				Api.Field.Update({field_id:selectedData.id,name:selectedData.name}).$promise.then(function(data){
						if(data.code==0)
	                     	vm.InfoModel('sm','操作成功','success')
	                  	else
	                    	vm.InfoModel('sm','操作失败','error')
						initSecondData(vm.fieldFirstList.selectedId)
				})
			}
		}

		function thridchildsave(obj){
			var selectedData;
			if(obj.addible){
				if(obj.insertdata.name==''){
					vm.InfoModel('sm','请填写字段名')
					return;
				}
				obj.addible=false;
				Api.Field.Put({parent_field_id:vm.fieldSecondList.selectedId,name:obj.insertdata.name}).$promise.then(function(data){
						if(data.code==0)
	                     	vm.InfoModel('sm','操作成功','success')
	                  	else
	                    	vm.InfoModel('sm','操作失败','error')
						obj.insertdata.name=''
						initThridData(vm.fieldSecondList.selectedId)
				})
			}
			else{
				for(var i in obj.data){
					if(obj.data[i].id==obj.selectedId){
						selectedData=obj.data[i];
						obj.data[i].editable=false;
						break;
					}
				}
				Api.Field.Update({field_id:selectedData.id,name:selectedData.name}).$promise.then(function(data){
						if(data.code==0)
	                     	vm.InfoModel('sm','操作成功','success')
	                  	else
	                    	vm.InfoModel('sm','操作失败','error')
						initThridData(vm.fieldSecondList.selectedId)
				})
			}
		}
		function fourchildsave(obj){
			var selectedData;
			if(obj.addible){
				if(obj.insertdata.name==''){
					vm.InfoModel('sm','请填写字段名')
					return;
				}
				obj.addible=false;
				Api.Field.Put({parent_field_id:vm.fieldThridList.selectedId,name:obj.insertdata.name}).$promise.then(function(data){
						if(data.code==0)
	                     	vm.InfoModel('sm','操作成功','success')
	                  	else
	                    	vm.InfoModel('sm','操作失败','error')
						obj.insertdata.name=''
						initFourData(vm.fieldThridList.selectedId)
				})
			}
			else{
				for(var i in obj.data){
					if(obj.data[i].id==obj.selectedId){
						selectedData=obj.data[i];
						obj.data[i].editable=false;
						break;
					}
				}
				Api.Field.Update({field_id:selectedData.id,name:selectedData.name}).$promise.then(function(data){
						if(data.code==0)
	                     	vm.InfoModel('sm','操作成功','success')
	                  	else
	                    	vm.InfoModel('sm','操作失败','error')
						initFourData(vm.fieldThridList.selectedId)
				})
			}
		}
		function load(){
				initSecondData(vm.fieldFirstList.selectedId)
		}
		function childload(){
				initThridData(vm.fieldSecondList.selectedId)
		}
		function prechildload(){
				initFourData(vm.fieldThridList.selectedId)
		}


		//model
		vm.EnsureModel=function openModel(size,item,callback){
			  var modalInstance = $uibModal.open({
			      animation: true,
			      templateUrl: 'EnsureModel',
			      controller: 'EnsureModelCtrl',
			      size: size,
			      resolve: {
			        item: function () {
			          return '删除'+item;
			        }
			      }
			    });

			    modalInstance.result.then(callback);
		}

		vm.InfoModel = function openModel(size, info,type,callback) {
			var modalInstance = $uibModal.open({
				animation: true,
				templateUrl: 'InfoModel',
				controller: 'InfoModelCtrl',
				size: size,
				resolve: {
					info: function() {
						return info;
					},
					type:function(){
						return type;
					}
				}
			});

			modalInstance.result.then(callback);
		}

		function initFourData(id){
			vm.fieldFourList.selectedId='';
			if(id){
				Api.Field.Query({parent_field_id:id}).$promise.then(function(data){
					angular.forEach(data.data,function(value,key){
							if(key==0){
								if(!vm.fieldFourList.selectedId){
									vm.fieldFourList.selectedId=value.id;
								}
							}
							value['editable']=false;
					})
					if(data.data.length<1){
						vm.fieldFourList.selectedId=''
					}
					$timeout(function(){vm.fieldFourList.data=data.data.sort(function(a,b){return a.id-b.id});;},0,true)

				})
			}
		}

		function initThridData(id){
			vm.fieldThridList.selectedId='';
			if(id){
				Api.Field.Query({parent_field_id:id}).$promise.then(function(data){
					angular.forEach(data.data,function(value,key){
							if(key==0){
								if(!vm.fieldThridList.selectedId){
									vm.fieldThridList.selectedId=value.id;
								}
							}
							value['editable']=false;
					})
					if(data.data.length<1){
						vm.fieldFourList.data=[]
					}
					else
						initFourData(vm.fieldThridList.selectedId)
					$timeout(function(){vm.fieldThridList.data=data.data.sort(function(a,b){return a.id-b.id});;},0,true)
				})
			}
		}

		function initSecondData(id){
			vm.fieldSecondList.selectedId='';
			if(id){
				Api.Field.Query({parent_field_id:id}).$promise.then(function(data){
					angular.forEach(data.data,function(value,key){
						if(key==0){
							if(!vm.fieldSecondList.selectedId){
								vm.fieldSecondList.selectedId=value.id;
							}
						}
						value['editable']=false;
					})
					if(data.data.length<1){
						vm.fieldThridList.data=[]
						vm.fieldFourList.data=[]
					}
					else
						initThridData(vm.fieldSecondList.selectedId)
					$timeout(function(){vm.fieldSecondList.data=data.data.sort(function(a,b){return a.id-b.id});;},0,true)
				})
			}
		}

		function initData(){
			Api.Field.Query().$promise.then(function(data){
				angular.forEach(data.data,function(value,key){
					if(key==0){
						if(!vm.fieldFirstList.selectedId){
							vm.fieldFirstList.selectedId=value.id;
						}
					}
					value['editable']=false;
				})
				if(data.data.length<1){
					vm.fieldFirstList.selectedId=''
				}
				initSecondData(vm.fieldFirstList.selectedId)
				$timeout(function(){vm.fieldFirstList.data=data.data.sort(function(a,b){return a.id-b.id});;},0,true)

			})
		}
	}
})();

